import { Controller } from "../../mvc/core/Controller";
import { IView } from "../../mvc/interfaces/IView";
import { ArenaGiftBagModule } from "../../../modules/ArenaGiftBagModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { IArenaGiftBagModule } from "../../../modules/interfaces/IArenaGiftBagModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { ItemData } from "../resource/info/ItemData";
import { GameProxy } from "../mvc/GameProxy";
import { ArenaGiftGetData } from "../view/arenaGiftbag/ArenaGiftGetData";
import { ClockManager } from "../manager/ClockManager";
import { ItemsUtil } from "../view/shortcuts/item/ItemsUtil";
import { SArenaGiftBagItem } from "../../../Message/Game/SArenaGiftBagItem";
import { SArenaGiftWish } from "../../../Message/Game/SArenaGiftWish";
type int = number;
//class ArenaGiftBagController
    
    export  class ArenaGiftBagController extends Controller
    {
        constructor()
        {
            
            super();this._ary = [];
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._arenaGiftBagModule) 
            {
                this._arenaGiftBagModule = new ArenaGiftBagModule();
                this._arenaGiftBagModule.addEventListener(WindowEvent.SHOW, this.onWindowShow);
                this._arenaGiftBagModule.addEventListener(WindowEvent.CLOSE, this.onWindowClose);
            }
            return this._arenaGiftBagModule;
        }

        /* internal  */onWindowShow(arg1: WindowEvent): void
        {
            this.updateMoney();
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            return;
        }

        /* internal  */onWindowClose(arg1: WindowEvent): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            (view as IArenaGiftBagModule).stopShake();
            return;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.PackOpenArenaGiftBag, this.onPackOpenArenaGiftBag);
            Dispatcher.addEventListener(EventName.ArenaGiftGet, this.onArenaGiftGet);
            Dispatcher.addEventListener(EventName.ArenaGiftRemoveConfirm, this.onArenaGiftRemove);
            NetDispatcher.addCmdListener(ServerCommand.ArenaGiftBagDataUpdate, this.onArenaGifgBagDataUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaGiftBagSelectUpdate, this.onArenaGiftBagSelectUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaGiftBagSelectNowUpdate, this.onArenaGiftBagSelectNowUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaGiftWishUpdate, this.onArenaGiftWishUpdateHandler);
            return;
        }

        /* internal  */updateMoney(arg1: Object=null): void
        {
            (view as IArenaGiftBagModule).updateMoney();
            return;
        }

        /* internal  */onPackOpenArenaGiftBag(arg1: DataEvent): void
        {
            var loc1=null;
            if ((view as IArenaGiftBagModule).isInShake) 
            {
                MsgManager.showRollTipsMsg(Language.getString(43117));
            }
            else 
            {
                loc1 = arg1.data as ItemData;
                this._itemData = loc1;
                if (loc1) 
                {
                    GameProxy.arenaGiftBag.openArenaGiftBag(loc1.uid);
                }
            }
            return;
        }

        /* internal  */onArenaGiftGet(arg1: DataEvent): void
        {
            var e: DataEvent;
            var nextItemData: ItemData;
            var nowTime: number;
            var autoGetData: ArenaGiftGetData;

            var loc1;
            nextItemData = null;
            nowTime = NaN;
            autoGetData = null;
            e = arg1;
            if (this._selectItemCode != 0) 
            {
                nowTime = ClockManager.instance.nowDate.time;
                if (!(this.lastGetTime == 0) && nowTime - this.lastGetTime < this.minGap) 
                {
                    return;
                }
                this.lastGetTime = ClockManager.instance.nowDate.time;
                GameProxy.arenaGiftBag.getArenaGift(this._itemData.uid);
                (view as IArenaGiftBagModule).flytoBag();
                if (e.data) 
                {
                    autoGetData = e.data as ArenaGiftGetData;
                    if (autoGetData.isAutoOpenNext) 
                    {
                        nextItemData = this.getArenaGiftNext(this._itemData.uid);
                        if (nextItemData) 
                        {
                            with ({}) 
                            {
                            };
                            
#error('pushwith') {}
                            .flash.utils.setTimeout(
#error('pushwith') {}
                            .a = (): void
                            {
                                Dispatcher.dispatchEvent(new DataEvent(EventName.PackOpenArenaGiftBag, nextItemData));
                                return;
                            }, 500)
                        }
                        else 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(43110));
                            (view as IArenaGiftBagModule).hide();
                        }
                    }
                    else 
                    {
                        (view as IArenaGiftBagModule).hide();
                    }
                }
                else if ((view as IArenaGiftBagModule).isAutoOpenNextItem) 
                {
                    nextItemData = this.getArenaGiftNext(this._itemData.uid);
                    if (nextItemData) 
                    {
                        with ({}) 
                        {
                        };
                        
#error('pushwith') {}
                        .flash.utils.setTimeout(
#error('pushwith') {}
                        .b = (): void
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.PackOpenArenaGiftBag, nextItemData));
                            return;
                        }, 500)
                    }
                    else 
                    {
                        (view as IArenaGiftBagModule).hide();
                    }
                }
                else 
                {
                    (view as IArenaGiftBagModule).hide();
                }
            }
            else 
            {
                GameProxy.arenaGiftBag.shakeArenaGift(this._itemData.uid);
            }
            return;
        }

        /* internal  */getArenaGiftNext(arg1: string): ItemData
        {
            var loc3=null;
            var loc1=cache.pack.backPackCache.getItemsByJudge(ItemsUtil.isArenaGiftBag);
            var loc2=0;
            while (loc2 < loc1.length) 
            {
                if ((loc3 = loc1[loc2] as ItemData).uid != arg1) 
                {
                    return loc3;
                }
                ++loc2;
            }
            return null;
        }

        /* internal  */onArenaGiftRemove(arg1: DataEvent): void
        {
            var loc1=arg1.data as SArenaGiftBagItem;
            var loc2=this._ary.indexOf(loc1);
            if (loc2 != -1) 
            {
                GameProxy.arenaGiftBag.removeArenaGift(this._itemData.uid, loc2);
            }
            return;
        }

        /* internal  */onArenaGifgBagDataUpdateHandler(arg1: Array<any>): void
        {
            this._ary = arg1;
            this.updateView();
            return;
        }

        /* internal  */onArenaGiftBagSelectUpdateHandler(arg1: int): void
        {
            this._selectItemCode = arg1;
            return;
        }

        /* internal  */onArenaGiftBagSelectNowUpdateHandler(arg1: int): void
        {
            this._selectItemCode = arg1;
            this.updateView(true);
            return;
        }

        /* internal  */onArenaGiftWishUpdateHandler(arg1: SArenaGiftWish): void
        {
            (view as IArenaGiftBagModule).updateWish(arg1.wish);
            return;
        }

        /* internal  */updateView(arg1: Boolean=false): void
        {
            (view as IArenaGiftBagModule).updateGifts(this._ary, this._selectItemCode, arg1);
            return;
        }

        private /* var */_arenaGiftBagModule: IArenaGiftBagModule;

        private /* var */_itemData: ItemData;

        private /* var */_ary: Array<any>;

        private /* var */_selectItemCode: int=0;

        private /* var */minGap: int=5000;

        private /* var */lastGetTime: number=0;
    }
